package org.keyintegrity.webbeans;

import java.math.BigDecimal;

import org.apache.tapestry.BaseComponent;
import org.keyintegrity.webbeans.utils.BeanUtils;

public abstract class BeanEditorSimpleTypeSupport extends BaseComponent {

    public abstract BeanEditorTypeMappings getTypeMapping();
    public abstract void setTypeMapping(BeanEditorTypeMappings value);

    public Class getBeanFieldType()     { return getTypeMapping().getBeanFieldType(); }
    private Object getValue()           { return getTypeMapping().getValue(); }
    private void setValue(Object value) { getTypeMapping().setValue(value); }
    private boolean isSimpleType()      { return getTypeMapping().isSimpleType(); }

    public String getSimpleTypeListEntryValueAsString() {
        return BeanUtils.getValueAsString(getValue());
    }
    
    public Object getEnumValue() {
        return getTypeMapping().getValue();
    }
    
    public void setEnumValue(Object value) {
        setValue(value);
    }

    public int getIntValue() {
        return ((Integer) getValue()).intValue();
    }
    
    public void setIntValue(int value) {
        setValue(value);
    }
    
    public double getDoubleValue() {
        return ((Double) getValue()).doubleValue();
    }
    
    public void setDoubleValue(double value) {
        setValue(value);
    }
    
    public boolean getBooleanValue() {
        return ((Boolean) getValue()).booleanValue();
    }
    
    public void setBooleanValue(boolean value) {
        setValue(value);
    }
    
    public String getStringValue() {
       return (String) getValue(); 
    }
    
    public void setStringValue(String value) {
        setValue(value);
    }
    
    public BigDecimal getBigDecimalValue() {
        return (BigDecimal) getValue();
    }
    
    public void setBigDecimalValue(BigDecimal value) {
        setValue(value);
    }
    
    public java.util.Date getDateValue() {
        return (java.util.Date) getValue();
    }
    
    public void setDateValue(java.util.Date value) {
        setValue(value);
    }
    
    private Class<?> getListEntryType() {
        return getValue().getClass();
    }
    
    public boolean isList() {
        return BeanUtils.isList(getBeanFieldType());
    }
    
    /*
     *  Simple types
     */
    
    public boolean isDouble() {
        return (! isSimpleType() && BeanUtils.isDouble(getListEntryType()))
        || BeanUtils.isDouble(getBeanFieldType());
    }
    
    public boolean isEnum() {
        return (! isSimpleType() && BeanUtils.isEnum(getListEntryType()))
        || BeanUtils.isEnum(getBeanFieldType());
    }
    
    public boolean isInt() {
        return (! isSimpleType() && BeanUtils.isInt(getListEntryType()))
            || BeanUtils.isInt(getBeanFieldType());
    }
    
    public boolean isBoolean() {
        return (! isSimpleType() && BeanUtils.isBoolean(getListEntryType()))
            || BeanUtils.isBoolean(getBeanFieldType());
    }
    
    public boolean isString() {
        return (! isSimpleType() && BeanUtils.isString(getListEntryType()))
            || BeanUtils.isString(getBeanFieldType());
    }
    
    public boolean isBigDecimal() {
        return (! isSimpleType() && BeanUtils.isBigDecimal(getListEntryType()))
            || BeanUtils.isBigDecimal(getBeanFieldType());
    }
    
    public boolean isDate() {
        return (! isSimpleType() && BeanUtils.isDate(getListEntryType()))
            || BeanUtils.isDate(getBeanFieldType());
    }
    
    public String getBeanFieldValueAsString() {
        return BeanUtils.getValueAsString(getTypeMapping().getBeanFieldValue());
    }
    
    public EnumSelectionModel getEnumSelectionModel() {
        //  TODO Use Cache pattern
        return new EnumSelectionModel(getBeanFieldType());
    }

}
